const db = require('../../db/index')
const uuid = require('uuid');

async function addCard (card, { packID, seriesID }) {
  let _card = {
    "url": card.url,
    "code": card.code,
    "packageCycle": card.package.packageCycle,
    "packageDetail": card.package.packageDetail,
    "cnName": card.name.cnName,
    "enName": card.name.enName,
    "jpName": card.name.jpName,
    "password": card.password,
    "typeOne": card.type.typeOne,
    "typeTwo": card.type.typeTwo,
    "attribute": card.attribute,
    "race": card.race,
    "rank": card.rank,
    "attack": card.atkAndDef.attack,
    "defense": card.atkAndDef.defense,
    "rare": card.rare,
    "usability": card.usability.OCG,
    "cnEffect": card.effect.cnEffect,
    "jpEffect": card.effect.jpEffect,
    "enEffect": card.effect.enEffect
  }
  try {
    // console.log(packID, seriesID);
    let ifNewCard = true
    let ifExit = await db.query(`select id from card_code where code='${_card.code}'`)
    // 如果编号存在,阻止进程
    if (ifExit[0]?.id) {
      return false
    }
    // //添加进入card
    let cardUUID = await db.query(`select id from card where password='${_card.password}'`)
    cardUUID = cardUUID[0]?.id
    if (!cardUUID) {
      cardUUID = uuid.v4().replace(/-/g, '')
      await db.query(`insert into card (id,password,cn_name,en_name,jp_name,type_one,type_two,attribute,race,rank,attack,defense,cn_effect,jp_effect,en_effect,after_carving) values ('${cardUUID}','${_card.password}','${_card.cnName.replace(/'/g, "''")}','${_card.enName.replace(/'/g, "''")}','${_card.jpName.replace(/'/g, "''")}','${_card.typeOne}','${_card.typeTwo}','${_card.attribute}','${_card.race}','${_card.rank}','${_card.attack}','${_card.defense}','${_card.cnEffect.replace(/'/g, "''")}','${_card.jpEffect.replace(/'/g, "''")}','${_card.enEffect.replace(/'/g, "''")}','0')`)
    } else {
      ifNewCard = false
      await db.query(`update card set after_carving='1' where id='${cardUUID}'`)
    }

    // //添加卡片编码,图片
    const cardCodeUUID = uuid.v4().replace(/-/g, '')
    await db.query(`insert into card_code (id,code,url) value('${cardCodeUUID}','${_card.code}','${_card.url}')`)

    //添加code,detail,cycle表
    const releaseCodeDetailCycleUUID = uuid.v4().replace(/-/g, '')
    await db.query(`insert into release_code_detail_cycle (id,code_id,detail_id,cycle_id) value('${releaseCodeDetailCycleUUID}','${cardCodeUUID}','${packID}','${seriesID}')`)

    //添加卡-卡片编号表
    const releaseCardCodeUUID = uuid.v4().replace(/-/g, '')
    await db.query(`insert into release_card_code (id,card_id,code_id) value('${releaseCardCodeUUID}','${cardUUID}','${cardCodeUUID}')`)

    // m 卡片限制 先查出id, 然后把牌和id写进关联表
    if (ifNewCard) {
      let usabilityUUID = await db.query(`select id from usability where usability='${_card.usability}'`)
      const releaseUsabilityCardUUID = uuid.v4().replace(/-/g, '')
      await db.query(`insert into release_card_usability (id,card_id,usability_id) value('${releaseUsabilityCardUUID}','${cardUUID}','${usabilityUUID[0].id}')`)
    }

    //code和卡片罕贵和数量关联(数量默认1),先根据罕贵查询id,如果id存在,直接关联,如果不存在,先添加到数据库,再关联
    _card.rare.split(',').map(async (item) => {
      let rareUUID = await db.query(`select id from rare where rare='${item.trim()}'`)
      rareUUID = rareUUID[0]?.id
      if (!rareUUID) {
        //没查到,添加该罕贵到数据库
        rareUUID = uuid.v4().replace(/-/g, '')
        await db.query(`insert into rare (id,rare) value ('${rareUUID}','${item.trim()}')`)
      }
      //将罕贵和code关联
      releaseCodeRareNumUUID = uuid.v4().replace(/-/g, '')
      await db.query(`insert into release_code_rare_number (id,code_id,rare_id,number) value ('${releaseCodeRareNumUUID}','${cardCodeUUID}','${rareUUID}','0')`)
    })

  } catch (error) {
    // console.log(
    //   `'${_card.password}','${_card.cnName.replace(/'/g, "''")}','${_card.enName.replace(/'/g, "''")}','${_card.jpName.replace(/'/g, "''")}','${_card.typeOne}','${_card.typeTwo}','${_card.attribute}','${_card.race}','${_card.rank}','${_card.attack}','${_card.defense}','${_card.cnEffect}','${_card.jpEffect}','${_card.enEffect.replace(/'/g, "''")}')`
    // );
    throw error
  }
}
async function addSeriesAndPack (series, pack, like) {
  try {
    let tag = {
      series: false,
      pack: false
    }
    let seriesID = await db.query(`select id from package_cycle where cycle_name='${series.trim()}'`)
    seriesID = seriesID[0]?.id
    if (!seriesID) {
      tag.series = true
      //没查到,添加该系列到数据库
      seriesID = uuid.v4().replace(/-/g, '')
      await db.query(`insert into package_cycle (id,cycle_name) value ('${seriesID}','${series}')`)
    }
    let packID = await db.query(`select id from package_detail where detail_name='${pack.trim()}'`)
    packID = packID[0]?.id
    if (!packID) {
      tag.pack = true
      //没查到,添加该卡包到数据库
      packID = uuid.v4().replace(/-/g, '')
      await db.query(`insert into package_detail (id,detail_name,detail_like) value ('${packID}','${pack}','${like}')`)
    }
    //当有一项新增的时候才会关联
    let releaseCycleAndDetailUUID = ''
    if (tag.pack || tag.series) {
      //将系列和卡包关联
      releaseCycleAndDetailUUID = uuid.v4().replace(/-/g, '')
      await db.query(`insert into release_package_cycle (id,cycle_id,detail_id) value ('${releaseCycleAndDetailUUID}','${seriesID}','${packID}')`)
    }
    return { packID, seriesID, releaseCycleAndDetailUUID, tag }
  } catch (error) {
    throw error
  }

}
async function removeErrorPAndC (data) {
  try {
    let { packID, seriesID, releaseCycleAndDetailUUID: releaseId, tag } = data
    if (tag.series) {
      await db.query(`delete from package_cycle where id = '${seriesID}'`)
    }
    if (tag.pack) {
      await db.query(`delete from package_detail where id = '${packID}'`)
    }
    if (releaseId) {
      await db.query(`delete from release_package_cycle where id = '${releaseId}'`)
    }
  } catch (error) {
    throw error
  }
}
module.exports = {
  addCard, addSeriesAndPack, removeErrorPAndC
}